package com.aynu.gulimall.product.service.impl;

import com.aynu.gulimall.product.entity.AttrEntity;
import com.aynu.gulimall.product.service.AttrService;
import com.aynu.gulimall.product.vo.AttrGroupwithAttrVo;
import com.aynu.gulimall.product.vo.SkuItemVo;
import com.aynu.gulimall.product.vo.SpuInfoGroupAttrVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aynu.common.utils.PageUtils;
import com.aynu.common.utils.Query;

import com.aynu.gulimall.product.dao.AttrGroupDao;
import com.aynu.gulimall.product.entity.AttrGroupEntity;
import com.aynu.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /*
    * QueryWrapper的方法，eq("catelog_id",catelogId),查看数据库字段 catelog_id 是否等于参数 catelogId
    *
    * */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        String key = (String) params.get("key");
        System.out.println(key);
        //select * from pms_attr_group where catelog_id = #{catelogId}
        //         and (attr_group_id=key or attr_group_name like key)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmptyOrWhitespaceOnly(key))
        {
            //查询条件and  【obj.eq("attr_group_id",key).or().like("attr_group_name",key); 】他结果
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if (catelogId == 0)
        {
            //调用IPage结构出来page所需要的属性值
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params)
                    ,wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params)
                    ,wrapper);
            return new PageUtils(page);
        }
    }

    /**
     *按id查询分组，并查询分组下的规格参数
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupwithAttrVo> getAttrGroupWithAttr(Long catelogId) {
        //1、查询分组
        List<AttrGroupEntity> groupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查询分组下的规格参数
        List<AttrGroupwithAttrVo> collect = groupEntities.stream().map(group -> {
            AttrGroupwithAttrVo attrRealtions = new AttrGroupwithAttrVo();
            BeanUtils.copyProperties(group, attrRealtions);
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(group.getAttrGroupId());
            attrRealtions.setAttrs(relationAttrs);
            return attrRealtions;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuInfoGroupAttrVo> getAttrGroupWithAttrsBySpuId(Long catalogId, Long spuId) {
        //1、查出当前对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        //1)、
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuInfoGroupAttrVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(catalogId,spuId);
        return vos;
    }

}